package com.mksujosu.main;

import java.util.ArrayList;

import org.andengine.engine.camera.Camera;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.scene.background.ParallaxBackground.ParallaxEntity;
import org.andengine.entity.shape.IAreaShape;
import org.andengine.opengl.util.GLState;

public class ParallayBackground extends Background {

	private final ArrayList<ParallayEntity> mParallayEntities = new ArrayList<ParallayEntity>();
	private int mParallayEntityCount;

	protected float mParallayValue;

	public ParallayBackground(final float pRed, final float pGreen,
			final float pBlue) {
		super(pRed, pGreen, pBlue);
	}

	public void setParallayValue(final float pParallayValue) {
		this.mParallayValue = pParallayValue;
	}

	@Override
	public void onDraw(GLState pGLState, Camera pCamera) {
		super.onDraw(pGLState, pCamera);
		final float parallaxValue = this.mParallayValue;
		final ArrayList<ParallayEntity> parallayEntities = this.mParallayEntities;

		for(int i = 0; i < this.mParallayEntityCount; i++) {
			parallayEntities.get(i).onDraw(pGLState, pCamera, parallaxValue);
		}
	}
	
	
	
	public void attachParallayEntity(final ParallayEntity pParallayEntity) {
		this.mParallayEntities.add(pParallayEntity);
		this.mParallayEntityCount++;
	}

	public boolean detachParallayEntity(final ParallayEntity pParallayEntity) {
		this.mParallayEntityCount--;
		final boolean success = this.mParallayEntities.remove(pParallayEntity);
		if(!success) {
			this.mParallayEntityCount++;
		}
		return success;
	}

	
	
	
	public static class ParallayEntity {

		final float mParallaxFactor;
		final IAreaShape mAreaShape;


		public ParallayEntity(final float pParallaxFactor, final IAreaShape pAreaShape) {
			this.mParallaxFactor = pParallaxFactor;
			this.mAreaShape = pAreaShape;
		}


		public void onDraw(final GLState pGLState, final Camera pCamera, final float pParallaxValue) {
			pGLState.pushModelViewGLMatrix();
			{
				/*final float cameraWidth = pCamera.getWidth();
				final float shapeWidthScaled = this.mAreaShape.getWidthScaled();
				float baseOffset = (pParallaxValue * this.mParallaxFactor) % shapeWidthScaled;

				while(baseOffset > 0) {
					baseOffset -= shapeWidthScaled;
				}
				pGLState.translateModelViewGLMatrixf(baseOffset, 0, 0);

				float currentMaxX = baseOffset;
				
				do {
					this.mAreaShape.onDraw(pGLState, pCamera);
					pGLState.translateModelViewGLMatrixf(shapeWidthScaled, 0, 0);
					currentMaxX += shapeWidthScaled;
				} while(currentMaxX < cameraWidth);*/
				
				
				final float cameraheight = pCamera.getHeight();
				final float shapeheightScaled = this.mAreaShape.getHeightScaled();
				float baseOffset = (pParallaxValue * this.mParallaxFactor) % shapeheightScaled;

				while(baseOffset > 0) {
					baseOffset -= shapeheightScaled;
				}
				pGLState.translateModelViewGLMatrixf(0, baseOffset, 0);

				float currentMaxX = baseOffset;
				
				do {
					this.mAreaShape.onDraw(pGLState, pCamera);
					pGLState.translateModelViewGLMatrixf(0, shapeheightScaled, 0);
					currentMaxX += shapeheightScaled;
				} while(currentMaxX < cameraheight);
				
				
				/*final float cameraHeight = pCamera.getHeight();
				final float shapeHeightScaled = 800;
				float baseOffset =(pParallaxValue * this.mParallaxFactor) % shapeHeightScaled;

				while(baseOffset > 0) {
					baseOffset -= shapeHeightScaled;
				}
				pGLState.translateModelViewGLMatrixf(0, baseOffset, 0);
				float currentMaxY = baseOffset;

				do {
					this.mAreaShape.onDraw(pGLState, pCamera);
					pGLState.translateModelViewGLMatrixf(0, shapeHeightScaled, 0);
					currentMaxY += shapeHeightScaled;
				} while(currentMaxY < cameraHeight);*/
				
			}
			pGLState.popModelViewGLMatrix();
		}

	}
}
